package com.mengdie.shuidi.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.event.ClickEvent;
import com.mengdie.shuidi.helper.UIHelper;
import com.mengdie.shuidi.ui.fragment.model.CollectModelFragment;
import com.mengdie.shuidi.ui.fragment.model.IPModelFragment;
import com.mengdie.shuidi.ui.fragment.model.UseModelFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 17/8/5 17:50
 * 邮箱：hqy_xz@126.com
 */

public class SeniorModelFragment extends Fragment {
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.tl_model_top)
    SlidingTabLayout mTlModelTop;
    @Bind(R.id.vp_model_bottom)
    ViewPager mVpModelBottom;

    @Bind(R.id.rl_generic_right)
    RelativeLayout mRlGenericRight;
    @Bind(R.id.iv_generic_right_one)
    ImageView mIvGenericRightOne;
    @Bind(R.id.iv_generic_right_two)
    ImageView mIvGenericRightTwo;
    private View rootView;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String mTitles[];
    private MyPagerAdapter mAdapter;
    private int num = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_model, container, false);
            ButterKnife.bind(this, rootView);
            init();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    private void init() {
        mTvGenericTitle.setText("高级模式");
        mRlGenericRight.setVisibility(View.VISIBLE);
        mIvGenericRightOne.setVisibility(View.VISIBLE);
        mFragments.add(IPModelFragment.newInstance("ip"));
        mFragments.add(CollectModelFragment.newInstance("collection"));
        mFragments.add(UseModelFragment.newInstance("record"));
        mTitles = getActivity().getResources().getStringArray(R.array.line_select_two);
        mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        mVpModelBottom.setAdapter(mAdapter);
        mTlModelTop.setViewPager(mVpModelBottom);
        mVpModelBottom.setCurrentItem(0);
        mVpModelBottom.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {
                if (position == 1) {
                    mIvGenericRightTwo.setVisibility(View.GONE);
                    mIvGenericRightOne.setVisibility(View.GONE);
                    mRlGenericRight.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            UIHelper.showCollectDialog(getActivity(), "2");
                        }
                    });
                } else if (position == 0) {
                    mIvGenericRightTwo.setVisibility(View.GONE);
                    mIvGenericRightOne.setVisibility(View.VISIBLE);
                    mRlGenericRight.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            EventBus.getDefault().post(new ClickEvent("1"));
                        }
                    });
                } else {
                    mIvGenericRightTwo.setVisibility(View.GONE);
                    mIvGenericRightOne.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(final int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @OnClick(R.id.rl_generic_back)
    public void onViewClicked() {
        getActivity().finish();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
